/**
 * 
 */
package elephant.servers.webserver.core;

import java.util.HashMap;
import java.util.Map;

import elephant.servers.webserver.view.View;
/**
 * 
 * @author icecooly
 *
 */
public class Context {
	public View view;
	public Throwable exception;
	public Request request;
	public Response response;
	public Map<String,Object>contextMap;
	public int retCode;
	//
	public Context() {
		contextMap=new HashMap<String, Object>();
	}
	//
	public void put(String key,Object v){
		contextMap.put(key, v);
	}
	//
	public View view(){
		return view;
	}
	//
	public View view(View v){
		this.view=v;
		return v;
	}
	//
	public Boolean getBoolean(String key){
		String ss=request.queryParams(key);
		return ss==null?null:Boolean.valueOf(ss);
	}
	//
	public Boolean getBoolean(String key,Boolean defaultValue){
		String ss=request.queryParams(key);
		return ss==null?defaultValue:Boolean.valueOf(ss);
	}
	//
	public String getString(String key){	
		String ss=request.queryParams(key);
		return ss;
	}
	//
	public String getString(String key,String defaultValue){	
		String ss=request.queryParams(key);
		return ss==null?defaultValue:ss;
	}
	//
	public Long getLong(String key){
		String ss=request.queryParams(key);
		return ss==null?null:Long.valueOf(ss);
	}
	//
	public Long getLong(String key,Long defaultValue){
		String ss=request.queryParams(key);
		return ss==null?defaultValue:Long.valueOf(ss);
	}
	//
	public Integer getInteger(String key){
		String ss=request.queryParams(key);
		return ss==null?null:Integer.valueOf(ss);
	}
	//
	public Integer getInteger(String key,Integer defaultValue){
		String ss=request.queryParams(key);
		return ss==null?defaultValue:Integer.valueOf(ss);
	}
	//
	public Short getShort(String key){
		String ss=request.queryParams(key);
		return ss==null?null:Short.valueOf(ss);
	}
	//
	public Short getShort(String key,Short defaultValue){
		String ss=request.queryParams(key);
		return ss==null?defaultValue:Short.valueOf(ss);
	}
	//
	public Float getFloat(String key){
		String ss=request.queryParams(key);
		return ss==null?null:Float.valueOf(ss);
	}
	//
	public Float getFloat(String key,Float defaultValue){
		String ss=request.queryParams(key);
		return ss==null?defaultValue:Float.valueOf(ss);
	}
	//
	public Double getDouble(String key){
		String ss=request.queryParams(key);
		return ss==null?null:Double.valueOf(ss);
	}
	//
	public Double getDouble(String key,Double defaultValue){
		String ss=request.queryParams(key);
		return ss==null?defaultValue:Double.valueOf(ss);
	}
	//
	@Override
	public String toString() {
		return request.toString();
	}
}
